package DataAccess;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;

import pojo.NhomPOJO;

public class NhomDA extends BaseDA {
	public NhomPOJO get(int id) {
		String sql = "select * from Nhom where ID=" + id;
		NhomPOJO rs = null;
		
		try {
			rs = new NhomPOJO();
			ResultSet rss = provider.executeQuery(sql);
			while (rss.next()) {
				rs.setId(rss.getInt("ID"));
				rs.setTenNhom(rss.getString("TenNhom"));
				rs.setThoiGian(new Date(rss.getTimestamp("ThoiGian").getTime()));
				rs.setIdNguoiTao(rss.getInt("IDNguoiTao"));
				break;
			}
		} catch (Exception e) {
			rs = null;
		}		
		return rs;
	}

	public ArrayList<NhomPOJO> getNhomNguoiDung(String tenDangNhap) {
		ArrayList<NhomPOJO> rs = null;
		String sql = "select n.* from Nhom n join Nhom_NguoiDung nnd on n.Id=nnd.IdNhom join NguoiDung nd on nd.Id=nnd.IdNguoiDung where nd.TenDangNhap='"
				+ tenDangNhap + "'";
		
		try{
			rs=new ArrayList<NhomPOJO>();
			ResultSet rss=provider.executeQuery(sql);
			while(rss.next()){
				NhomPOJO nhom=new NhomPOJO();
				nhom.setId(rss.getInt("ID"));
				nhom.setTenNhom(rss.getString("TenNhom"));
				nhom.setThoiGian(new Date(rss.getTimestamp("ThoiGian").getTime()));
				nhom.setIdNguoiTao(rss.getInt("IDNguoiTao"));
				rs.add(nhom);
			}
		}catch (Exception e) {
			rs=null;
		}
		
		return rs;
	}
	
	public ArrayList<NhomPOJO> getNhomNguoiDung(int idNguoiDung) {
		ArrayList<NhomPOJO> rs = null;
		String sql = "select * from nhom n join nhom_nguoidung nnd on n.id=nnd.idnhom where nnd.idnguoidung="+idNguoiDung;
		
		try{
			rs=new ArrayList<NhomPOJO>();
			ResultSet rss=provider.executeQuery(sql);
			while(rss.next()){
				NhomPOJO nhom=new NhomPOJO();
				nhom.setId(rss.getInt("ID"));
				nhom.setTenNhom(rss.getString("TenNhom"));
				nhom.setThoiGian(new Date(rss.getTimestamp("ThoiGian").getTime()));
				nhom.setIdNguoiTao(rss.getInt("IDNguoiTao"));
				rs.add(nhom);
			}
		}catch (Exception e) {
			rs=null;
		}
		
		return rs;
	}

	@Override
	protected Object getProperties(ResultSet rss) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}
}
